package com.liujit.pay.wx.domain.params.order;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import lombok.Builder;

import java.util.List;

/**
 * @description: 优惠功能
 * @author: liujun
 * @create: 2021-02-20 09:56
 */
@Builder
public class OrderDetail {

    /**
     * 订单原价
     */
    private Integer costPrice;

    /**
     * 商品小票ID
     */
    private String invoiceId;

    /**
     * 商品列表
     */
    private List<OrderGoodsDetail> orderGoodsDetailList;

    /**
     * 转化JSON字符串
     *
     * @return
     */
    public String toJSONString() {
        JSONObject json = new JSONObject();
        // 订单原价
        if (costPrice != null) {
            json.set("cost_price", costPrice);
        }
        // 商品小票ID
        if (StrUtil.isNotBlank(invoiceId)) {
            json.set("invoice_id", invoiceId);
        }
        // 商品列表
        if (orderGoodsDetailList != null && orderGoodsDetailList.size() > 0) {
            JSONArray array = new JSONArray();
            for (OrderGoodsDetail detail : orderGoodsDetailList) {
                array.set(detail.toJSONObject());
            }
            json.set("goods_detail", array.toString());
        }
        return json.toString();
    }
}
